uniform sampler2D tex;
uniform int size;
uniform float causticsIntensity;
uniform bool showTex;

//const int ks = 7;
//const int kt = 5;

void main(void)
{
	int i, j;
	float grid = 1.0 / float(size);
	vec3 color = vec3(0.0);
	int ks, kt;

	ks = 7 + int(gl_TexCoord[0].s * 8.0 - 4.0);
	kt = 5 + int(gl_TexCoord[0].s * 2.0 - 1.0);
	
	for (i = -kt; i <= kt; i++) {
		for (j = -ks; j <= ks; j++) {
			vec2 offset = vec2(grid * float(j), grid * float(i));
			vec3 temp = vec3(texture2D(tex, gl_TexCoord[0].st + offset));
			//if (temp.x < causticsIntensity) temp = vec3(causticsIntensity);
			color += temp;
		}
	}
	color /= float((2 * ks + 1) * (2 * kt + 1));
	if (!showTex) {
		color -= vec3(causticsIntensity);
	}
	//color = vec3(texture2D(tex, gl_TexCoord[0].st));
	
	gl_FragColor = vec4(color, 1.0);
}